package com.monika.service.controller;

import cn.hutool.http.server.HttpServerResponse;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.monika.main.system.util.doc.PDF;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;

/**
 * @author:whh
 * @date: 2025-05-09 21:49
 * <p></p>
 */
@RequestMapping("/file")
@Controller
public class FileController {


    String path="doc";


    @GetMapping("/pdf/{fileName}")
    public void pdf(@PathVariable("fileName")String fileName, HttpServletResponse response){
        File file = new File(path + File.separator + fileName);

        try (PDF pdf = new PDF(file);) {
            byte[] bytes = pdf.PDFPage(1);
            // 设置响应头
            response.setContentType("application/pdf");
            response.setContentLength(bytes.length);
            // 将PDF内容写入响应的输出流
            try (OutputStream outputStream = response.getOutputStream()) {
                outputStream.write(bytes);
                outputStream.flush();
            }catch (Exception e){
                e.printStackTrace();
            }
        }catch (Exception e){
            e.printStackTrace();
        }


    }




    @GetMapping("/pdf_/{fileName}")
    public String  pdf_(@PathVariable("fileName")String fileName, HttpServletResponse response){

        String ff = path + "/"+ fileName;

        return "redirect:/"+String.format("pdf/web/viewer.html?file=2.pdf",ff );


    }
}
